package mysql

import (
	"short-video-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int    `gorm:"type:int;not null;comment:视频ID"`
	UserId         int    `gorm:"type:int;not null;comment:用户ID"`
	CommentContent string `gorm:"type:varchar(500);not null;comment:评论内容"`
	Anonymous      int    `gorm:"type:tinyint;not null;comment:是否匿名"`
	CommentStatus  int    `gorm:"type:int(11);comment:评论状态"`
}

func (c *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *CommentInfo) CommentById(db *gorm.DB, in *videorpc.CommentDelRequest) error {
	return db.Debug().Where("user_id=? and video_id=?", in.UserId, in.VideoId).First(c).Error
}

func (c *CommentInfo) CommentDel(db *gorm.DB) error {
	return db.Debug().Delete(c).Error
}

func CommentList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CommentList {
	var list []*videorpc.CommentList
	db.Debug().Model(&CommentInfo{}).Select("nick_name,video_title as title,comment_content,anonymous").
		Joins("LEFT JOIN user_infos ON comment_infos.user_id = user_infos.id").
		Joins("LEFT JOIN video_infos ON comment_infos.video_id=video_infos.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
